/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO.Wordpress;

import MySqlAccess.Article;
import MySqlAccess.ArticleInfo;
import MySqlAccess.RatingInfo;
import MySqlAccess.UserRating;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *
 * @author tran tuan tai
 */
public class RepairDatePost
{
    Timestamp beginDate = new Timestamp(0, 0, 1, 0, 0, 0, 0);
    Timestamp endDate = new Timestamp(111, 5, 4, 0, 0, 0, 0);

    public void Repair()
    {
        Article article = new Article();
        UserRating userRating = new UserRating();
        List<ArticleInfo> lstArticle = article.GetByDate(beginDate, endDate);
        for (ArticleInfo  info : lstArticle)
        {
            List<RatingInfo> ratingInfo = userRating.GetItemByPostID(info.id);
            Date minTime = null;

            if (ratingInfo.size() > 0)
            {
                minTime = ratingInfo.get(0).m_dateTime;
                for (int j = 1; j < ratingInfo.size(); j++)
                {
                    if (minTime.compareTo(ratingInfo.get(j).m_dateTime) == 1)
                    {
                        minTime = ratingInfo.get(j).m_dateTime;
                    }
                }
            }

             if (ratingInfo.size() > 0)
                {
                    article.UpdateDate(info.id, new Timestamp(minTime.getTime()));
                }
                else
                {
                    article.UpdateDate(info.id, new Timestamp(111, 5, 8, 0, 0, 0, 0));
                }

        }
        System.out.println(lstArticle.size());
    }

    public static void main(String[] argvs)
    {
        RepairDatePost instance = new RepairDatePost();
        instance.Repair();
    }
}
